package org.openpop.TouristDemo.DB;

import android.content.ContentValues;
import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DBAdapter {
	private Context mContext=null;
	private DatabaseHelper mDatabaseHelper=null;
	private SQLiteDatabase mSQLiteDatabase=null;
	private static final String DB_CREATE_CONTACT="create table ContactInfo" +
			"(ContactID integer primary key autoincrement," +
			"Name varchar(10)," +
			"Sex varchar(2)," +
			"DeviceID integer," +
			"IsFriend integer," +
			"lng double," +
			"lat double);";
	private static final String DB_CREATE_MSG="create table MessageInfo" +
			"(messageID integer primary key autoincrement," +
			"TypeID integer," +
			"IDA integer," +
			"IDB integer," +
			"DateTime datetime," +
			"Context carchar);";
	
	private static class DatabaseHelper extends SQLiteOpenHelper{
		public DatabaseHelper(Context context) {
			super(context, "tourist.db", null, 1);
		}
		@Override
		public void onCreate(SQLiteDatabase db){
			db.execSQL(DB_CREATE_CONTACT);
			db.execSQL(DB_CREATE_MSG);
		}
		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			db.execSQL("DROP TABLE IF EXISTS notes");
			onCreate(db);
		}
	}
	public DBAdapter(Context context){
		mContext=context;
	}
	public void open() throws SQLException{
		mDatabaseHelper=new DatabaseHelper(mContext);
		mDatabaseHelper.getWritableDatabase();
	}
	public void close(){
		mDatabaseHelper.close();
	}
	public long insertData(int num,int type,int ida,int idb){
		ContentValues initialValues=new ContentValues();
		initialValues.put("MessageID",num);
		initialValues.put("TypeID", type);
		initialValues.put("IDA", ida);
		initialValues.put("IDB", idb);
		return mSQLiteDatabase.insert(DB_CREATE_MSG, "MessageID", initialValues);
	}
}